package com.guducat.collegeWeb.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 用户数据同步请求DTO
 * 用于处理用户收藏、历史记录等个人数据的同步
 */
@Data
@Schema(description = "用户数据同步请求对象")
public class UserDataSyncDTO {

    @Schema(description = "同步操作类型", example = "upload", allowableValues = {"upload", "download", "merge"})
    @NotBlank(message = "同步操作类型不能为空")
    private String syncType;

    @Schema(description = "数据类型列表", example = "[\"favorite\", \"history\"]")
    @NotNull(message = "数据类型不能为空")
    @Size(min = 1, message = "至少需要指定一种数据类型")
    private List<String> dataTypes;

    @Schema(description = "客户端版本", example = "1.0.0")
    private String clientVersion;

    @Schema(description = "最后同步时间", example = "2024-01-15T10:30:00")
    private LocalDateTime lastSyncTime;

    @Schema(description = "用户数据列表")
    @Valid
    private List<UserDataItemDTO> dataItems;

    /**
     * 用户数据项DTO
     */
    @Data
    @Schema(description = "用户数据项")
    public static class UserDataItemDTO {

        @Schema(description = "数据类型", example = "favorite", allowableValues = {"favorite", "history", "like"})
        @NotBlank(message = "数据类型不能为空")
        private String dataType;

        @Schema(description = "文章ID", example = "1001")
        @NotNull(message = "文章ID不能为空")
        private Integer newsId;

        @Schema(description = "文章标题", example = "校园新闻标题")
        private String newsTitle;

        @Schema(description = "文章URL", example = "/news/1001")
        private String newsUrl;

        @Schema(description = "文章分类", example = "校园新闻")
        private String newsCategory;

        @Schema(description = "文章所属行政级别", example = "学院级")
        private String newsSection;

        @Schema(description = "扩展数据", example = "{\"tags\": [\"校园\", \"新闻\"], \"note\": \"很有用的文章\"}")
        private Map<String, Object> extraData;

        @Schema(description = "创建时间", example = "2024-01-15T10:30:00")
        private LocalDateTime createTime;

        @Schema(description = "更新时间", example = "2024-01-15T10:30:00")
        private LocalDateTime updateTime;
    }

    /**
     * 同步配置DTO
     */
    @Data
    @Schema(description = "同步配置")
    public static class SyncConfigDTO {

        @Schema(description = "是否覆盖冲突数据", example = "false")
        private Boolean overwriteConflicts = false;

        @Schema(description = "最大同步数量", example = "1000")
        private Integer maxSyncCount = 1000;

        @Schema(description = "是否删除本地不存在的数据", example = "false")
        private Boolean deleteLocalMissing = false;

        @Schema(description = "冲突解决策略", example = "keep_latest", allowableValues = {"keep_latest", "keep_server", "keep_client"})
        private String conflictResolution = "keep_latest";
    }

    @Schema(description = "同步配置")
    @Valid
    private SyncConfigDTO syncConfig;
}
